package cn.yllr.tomgxianghui.controller;

import cn.yllr.tomgxianghui.entity.Dishes;
import cn.yllr.tomgxianghui.model.ShowCook;
import cn.yllr.tomgxianghui.service.DishesService;
import cn.yllr.tomgxianghui.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Dishes)表控制层
 *
 * @author makejava
 * @since 2021-12-21 19:49:44
 */
@Controller
@RequestMapping("dishes")
public class DishesController {
    /**
     * 服务对象
     */
    @Resource
    private DishesService dishesService;

    @Resource
    private UserService userService;

    /**
     * 封装渲染页面
     * @param leftInfo
     * @param rightInfo1
     * @param rightInfo2
     * @param api1
     * @param api2
     * @param model
     */
    public void render(String leftInfo, String rightInfo1, String rightInfo2, String api1, String api2,
                       Model model) {
        model.addAttribute("leftInfo", leftInfo);
        model.addAttribute("rightInfo1", rightInfo1);
        model.addAttribute("rightInfo2", rightInfo2);
        model.addAttribute("api1", api1);
        model.addAttribute("api2", api2);
    }

    /**
     * 首页跳转，推荐10道菜，推荐10个厨师
     *
     * @param model
     * @return
     */
    @RequestMapping("recommend")
    public String recommend(Model model) {
        // 通过销量推荐十道菜
        List<Dishes> dishes = dishesService.queryDishesRecommend(10);
        /*随机返回十个厨师，厨师还没有评价系统*/
        List<ShowCook> cooks = userService.selectRecommendCooks(10);
        render("精品推荐", "查看全部菜品", "查看厨师推荐", "/dishes/selectAllDishes", "/user/selectRecommendCooks", model);
        model.addAttribute("dishes", dishes);
        model.addAttribute("cooks", cooks);
        return "/index";
    }

    /**
     * 异步刷新，查询推荐菜品
     *
     * @param model
     * @return
     */
    @RequestMapping("syncRecommend")
    public String syncRecommend(Model model) {
        List<Dishes> dishes = dishesService.queryDishesRecommend(10);
        render("精品推荐", "查看全部菜品", "查看厨师推荐", "/dishes/selectAllDishes", "/user/selectRecommendCooks", model);
        model.addAttribute("dishes", dishes);
        return "/index::showInfo";
    }

    /**
     * 异步刷新，查询全部菜品
     *
     * @param model
     * @return
     */
    @RequestMapping("/selectAllDishes")
    public String selectAll(Model model) {
        List<Dishes> dishes = dishesService.findAllDishes();
        render("所有菜品", "查看精品推荐", "查看全部厨师", "/dishes/syncRecommend", "/user/selectAllCooks", model);
        model.addAttribute("dishes", dishes);
        return "/index::showInfo";
    }

    /**
     * 异步刷新，通过关键词查询菜品
     *
     * @param model
     * @param name
     * @return
     */
    @RequestMapping("selectDishesByName")
    public String selectDishesByName(Model model, String name) {
        List<Dishes> dishes = dishesService.queryDishesByName(name);
        render("菜品搜索结果", "查看精品推荐", "查看全部厨师", "/dishes/syncRecommend", "/user/selectAllCooks", model);
        model.addAttribute("dishes", dishes);
        return "/index::showInfo";
    }
}